﻿using System;
using System.Windows.Input;
using ClientOverflow.Core;
using ClientOverflow.DataTransferObjects;

namespace ClientOverflow.ViewModels.Register
{
    /// <summary>
    /// SaveCommand is a command that saves a new user to the DB. The command ovserves the user, which is the
    /// model of the UserRegistrationViewModel. When the command is executed the WCF service is called to add
    /// the user to the DB. In addition, the command automatically logs in the new user.
    /// </summary>
    internal class SaveCommand : ICommand
    {
        private readonly User user;

        public SaveCommand(User user)
        {
            this.user = user;
        }

        public void Execute(object parameter)
        {
            using (var usersServiceClient = ServicesFactory.CreateClientOverflowServiceClient())
            {
                usersServiceClient.RegisterNewUser(user);
            }

            ServicesProviderFactory.GetInstance().LoginService.Login(user);
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }
}